<?php
/**
 * 中文 - 拼音类
 * @copyright reginx.com
 * $Id$
 */
class py_lib {

    /**
     * 获取字符串拼音 
     *
     * @param unknown_type $cnstr
     */
    public static function get($scnstr , $pstr = '-'){
        $ret = '';
        $cnstr = preg_replace('/\s*/' , '' , $scnstr ? $scnstr : ' ');
        if($cnstr != ''){
            $cnstr = empty($cnstr) ? '' : str_replace($pstr , '' , trim($cnstr));
            $lst = self::split($cnstr);
            $ret = join($pstr , $lst);
            $res = core::getobj('py_tab')->where("cn in ('" . join('\',\'' , $lst) . "')")->getall();
            foreach ($res as $k => $v){
                $ret = str_replace($v['cn'] , $v['py'] , $ret);
            }
            unset($lst);
        }else{
            $ret = $scnstr;
        }
        return $ret;
    }

    /**
     * 宽字符串分割 (utf-8)
     *
     * @param unknown_type $str
     * @return unknown
     */
    public static function split($str){
        $str = preg_replace('/[^\x{4e00}-\x{9fa5}\w]+/u' , '' , $str);
        $str = preg_split('/(?<!^)(?!$)/u', $str);
        $str = preg_replace('/\-?(\w+)\-?/' , '\1' , join('-', $str));
        $str = preg_replace('/\w+/' , '-\0-' , $str);
        return explode('-' , $str);
    }
}
?>